from typing import Dict
from dataclasses import dataclass
import sglang as sgl

from .logger import init_logger
logger = init_logger(__name__)
@dataclass
class APIConfig:
    def __init__(self, model_name: str, api_key: str, base_url: str):
        self.model_name = model_name
        self.api_key = api_key
        self.base_url = base_url


class APIConfigFactory:
    _configurations: Dict[str, APIConfig] = {
        "deepseek-r1": APIConfig(
            model_name="deepseek-reasoner",
            api_key="sk-c80e688b2d544f16825ceca256d3c6e2",
            base_url="https://api.deepseek.com",
        ),
        "huoshan-deepseek-r1": APIConfig(
            model_name="deepseek-r1-250120",
            api_key="9b1afd3f-bbe8-493f-9ba4-c17784556f0c",
            base_url="https://ark.cn-beijing.volces.com/api/v3",
        ),
        "huoshan-deepseek-r1-distill-qwen-7b": APIConfig(
            model_name="deepseek-r1-distill-qwen-7b-250120",
            api_key="9b1afd3f-bbe8-493f-9ba4-c17784556f0c",
            base_url="https://ark.cn-beijing.volces.com/api/v3",
        ),
        "qwq-official": APIConfig(
            model_name="qwq-32b",
            api_key="sk-8db036d3267f461bb042c9c9b14d8143",
            base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
        ),
    }

    @classmethod
    def list_available_configs(cls):
        return list(cls._configurations.keys())

    @classmethod
    def get_llm_api(cls, api_name: str) -> APIConfig:
        return cls._configurations.get(api_name, cls._configurations["deepseek-r1"])

def setup_default_sglang_backend(api_config:APIConfig,is_chat_model = True):
    sgl.set_default_backend(
        sgl.OpenAI(
            api_config.model_name,
            base_url=api_config.base_url,
            api_key=api_config.base_url,
            is_chat_model=is_chat_model,
        )
    )
    logger.info(f"sglang default backend = {sgl.global_config.default_backend}")